package myReflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class Reflect02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class<?> clazz =Student.class;
        System.out.println(clazz);
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        Field age = clazz.getDeclaredField("age");
        System.out.println(age);
        //获取权限
        int modifiers = age.getModifiers();
        System.out.println(modifiers);
        //获取成员变量的名字
        String ageName = age.getName();
        System.out.println(ageName);
        //获取成员变量的类型
        Class<?> ageType = age.getType();
        System.out.println(ageType);
        //获取变量的值
        Student student = new Student("sb",15);
        age.setAccessible(true);
        int Age = (int) age.get(student);
        System.out.println(Age);
        //修改变量的值
        age.set(student,99);
        System.out.println(age.get(student));
    }
}
